package com.spider.silence.worker;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.spider.silence.downloader.BaiduForumDownloader;
import com.spider.silence.output.*;
import com.spider.silence.pool.BaiduForumDownLoaderPool;
import com.spider.silence.task.BaiduForumTask;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.DateUtils;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Element;

import java.io.File;
import java.util.HashMap;
import java.util.Map;


/**
 * 百度贴吧Worker
 * 每个贴吧开启一个线程
 * Created by 赵欢 on 2017/1/9.
 */
public class BaiduForumWorker implements Runnable {

    private static Logger logger = Logger.getLogger(BaiduForumWorker.class);
    @Override
    public void run(){
    	while(true && !Thread.interrupted()){
            BaiduForumDownLoaderPool pool = BaiduForumDownLoaderPool.getInstance();
            BaiduForumDownloader downloader = null;
//            MongoWriter writer = null;
            FileWriter writer = null;
            FileWriterPool writerPool =FileWriterPool.getInstance();
            try {
                downloader = pool.fetchDownLoader(1000);
                if(downloader != null) {
                    BaiduForumTask task =(BaiduForumTask) CommonTaskManager.takeTask("baiduForumTask",1000);
                    if(task == null) {
                        continue;
                    }
                    //贴吧名称
                    String forumName = task.getForumName();
                    String url = task.getUrl();
                    String tId = task.gettId();
                    JSONObject result = downloader.crawlItem(url,tId,task.getType());
                    if(result != null && result.size()>0) {
                        String filePrefix = "BaiduForums";//文件前缀
                        JSONObject data = result.getJSONObject("data");
                        long tsp = DateUtils.dateToTimestamp(DateUtils.parseDateTimeOnModel(data.getString("publish_time"), DateUtils.DATETIME_FORMAT_MM)).getTime();//发布时间
                        String tid = data.getString("id"); //帖子ID
                        String user_id = data.getString("publish_user_id");//用户名称
                        String fileName = filePrefix + "-" + forumName + "_" + tsp + "_" + user_id + "_" + tid;//平台名称（或者英文）_ 言论发布的时间戳 _ 发布用户的ID _ 言论的tid
                        String publishDate = DateUtils.formatDate(DateUtils.parseDate(data.getString("publish_time"))).replace("-", "-");
                        writer = writerPool.fetchWriter();
                        Map<String, File> map = downloader.getFiles();
                        writer.write("百度贴吧_"+forumName+"_" +"speeches_"+ publishDate, fileName, JSON.toJSONString(result, SerializerFeature.BrowserCompatible),map);
                        System.out.println(data.toJSONString());
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(downloader!=null){
                    pool.releaseDownLoader(downloader);
                }
                if(writer!=null){
                    writerPool.releaseWriter(writer);
                }
            }
        }
    }
}


